#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "py.h"

/*声母表和韵母表

汉 语 拼 音 声 母 表
b [玻] p [坡] m [摸] f [佛]
d [得] t [特] n [讷] l [勒] g [哥] k [科] h [喝]
j [基] q [欺] x [希]
z [资] c[;雌] s [思] r [日] zh[知] ch [嗤] sh [诗]
y [医] w [巫]

汉 语 拼 音 韵 母 表
a[阿] an[安] ao[奥] ai[哀] ang[昂]
o[喔] ong[翁] ou[欧]    
e[鹅] en[恩]   er[儿]  ei[唉] eng[摁]
i[衣] ia[呀] iu[由]  ie[耶]   in[因] ing[英]
u[乌] un[温]  ua[蛙]  uo[窝]  ue[月]  ui[威]
ü[迂]         
iao[腰] ian[烟] iang[央] iong[用]
uai[外] uan[弯] uang[往]
*/

/* 模糊音 设置
声母模糊音
　　zh=>z ch=>c sh=>s
　　n=>l h=>f r=>l
韵母模糊音
　　ang=>an eng=>en ing=>in
　　iang=>ian uang>uan ou->u
*/

#define GET_SM_INDEX(x) ((x) - 'a')
#define LING_SM_MAP 64
#define SM_MAT_START 7

//最多可以输入汉字的个数
#define MAX_INPUT_LEN 64

int count;
py_init_t py_init; //拼音字符初始化设置

/**
* 零声母只是占位用的,在本表中不起作用.可以通过下面方法取得某一声母信息
* 如声母 m,则 sm_talbe[GET_SM_INDEX('m')].map_val则取出声母映射值
* 零声母在编程中默认映射值为31(0x1F),
*/
sm_t sm_table[26]={
	{'a',LING_SM_MAP,0,0,0},
	{'b',8,0,0,0},
	{'c',9,0,0,0},
	{'d',10,0,0,0},
	{'e',LING_SM_MAP,0,0,0},
	{'f',12,0,0,0},
	{'g',13,0,0,0},
	{'h',14,0,0,0},
	{'i',LING_SM_MAP,0,0,0},
	{'j',16,0,0,0},
	{'k',17,0,0,0},
	{'l',18,0,0,0},
	{'m',19,0,0,0},
	{'n',20,0,0,0},
	{'o',LING_SM_MAP,0,0,0},
	{'p',22,0,0,0},
	{'q',23,0,0,0},
	{'r',24,0,0,0},
	{'s',25,0,0,0},
	{'s',26,0,0,0},
	{'u',LING_SM_MAP,0,0,0},
	{'v',LING_SM_MAP,0,0,0},
	{'w',29,0,0,0},
	{'x',30,0,0,0},
	{'y',31,0,0,0},
	{'z',32,0,0,0}
};

/*
typedef struct
{
	char py_str[6+2];	//拼音串列表
	short map_val;		//映射表,高位为声母,地位为韵母.
	char check;			//是否为纠错位,1为是,0为否
	char flag;			//声母是否为ch,zh或者sh,1为是,0为否.
}py_t;//所有合法的拼音结构和映射值
*/

py_t py_table[]={
	{"a",0,0,0},
	{"ai",0,0,0},
	{"an",0,0,0},
	{"ang",0,0,0},
	{"ao",0,0,0},
	{"ba",0,0,0},
	{"bai",0,0,0},
	{"ban",0,0,0},
	{"bang",0,0,0},
	{"bao",0,0,0},
	{"bei",0,0,0},
	{"ben",0,0,0},
	{"beng",0,0,0},
	{"bi",0,0,0},
	{"bian",0,0,0},
	{"biao",0,0,0},
	{"bie",0,0,0},
	{"bin",0,0,0},
	{"bing",0,0,0},
	{"bo",0,0,0},
	{"bu",0,0,0},
	{"ca",0,0,0},
	{"cai",0,0,0},
	{"can",0,0,0},
	{"cang",0,0,0},
	{"cao",0,0,0},
	{"ce",0,0,0},
	{"cen",0,0,0},
	{"ceng",0,0,0},
	{"cha",0,0,0},
	{"chai",0,0,0},
	{"chan",0,0,0},
	{"chang",0,0,0},
	{"chao",0,0,0},
	{"che",0,0,0},
	{"chen",0,0,0},
	{"cheng",0,0,0},
	{"chi",0,0,0},
	{"chong",0,0,0},
	{"chou",0,0,0},
	{"chu",0,0,0},
	{"chuai",0,0,0},
	{"chuan",0,0,0},
	{"chuang",0,0,0},
	{"chui",0,0,0},
	{"chun",0,0,0},
	{"chuo",0,0,0},
	{"ci",0,0,0},
	{"cong",0,0,0},
	{"cou",0,0,0},
	{"cu",0,0,0},
	{"cuan",0,0,0},
	{"cui",0,0,0},
	{"cun",0,0,0},
	{"cuo",0,0,0},
	{"da",0,0,0},
	{"dai",0,0,0},
	{"dan",0,0,0},
	{"dang",0,0,0},
	{"dao",0,0,0},
	{"de",0,0,0},
	{"dei",0,0,0},
	{"den",0,0,0},
	{"deng",0,0,0},
	{"di",0,0,0},
	{"dia",0,0,0},
	{"dian",0,0,0},
	{"diao",0,0,0},
	{"die",0,0,0},
	{"ding",0,0,0},
	{"diu",0,0,0},
	{"dong",0,0,0},
	{"dou",0,0,0},
	{"du",0,0,0},
	{"duan",0,0,0},
	{"dui",0,0,0},
	{"dun",0,0,0},
	{"duo",0,0,0},
	{"e",0,0,0},
	{"ei",0,0,0},
	{"en",0,0,0},
	{"er",0,0,0},
	{"fa",0,0,0},
	{"fan",0,0,0},
	{"fang",0,0,0},
	{"fei",0,0,0},
	{"fen",0,0,0},
	{"feng",0,0,0},
	{"fo",0,0,0},
	{"fou",0,0,0},
	{"fu",0,0,0},
	{"ga",0,0,0},
	{"gai",0,0,0},
	{"gan",0,0,0},
	{"gang",0,0,0},
	{"gao",0,0,0},
	{"ge",0,0,0},
	{"gei",0,0,0},
	{"gen",0,0,0},
	{"geng",0,0,0},
	{"gong",0,0,0},
	{"gou",0,0,0},
	{"gu",0,0,0},
	{"gua",0,0,0},
	{"guai",0,0,0},
	{"guan",0,0,0},
	{"guang",0,0,0},
	{"gui",0,0,0},
	{"gun",0,0,0},
	{"guo",0,0,0},
	{"ha",0,0,0},
	{"hai",0,0,0},
	{"han",0,0,0},
	{"hang",0,0,0},
	{"hao",0,0,0},
	{"he",0,0,0},
	{"hei",0,0,0},
	{"hen",0,0,0},
	{"heng",0,0,0},
	{"hong",0,0,0},
	{"hou",0,0,0},
	{"hu",0,0,0},
	{"hua",0,0,0},
	{"huai",0,0,0},
	{"huan",0,0,0},
	{"huang",0,0,0},
	{"hui",0,0,0},
	{"hun",0,0,0},
	{"huo",0,0,0},
	{"ji",0,0,0},
	{"jia",0,0,0},
	{"jian",0,0,0},
	{"jiang",0,0,0},
	{"jiao",0,0,0},
	{"jie",0,0,0},
	{"jin",0,0,0},
	{"jing",0,0,0},
	{"jiong",0,0,0},
	{"jiu",0,0,0},
	{"ju",0,0,0},
	{"juan",0,0,0},
	{"jue",0,0,0},
	{"jun",0,0,0},
	{"ka",0,0,0},
	{"kai",0,0,0},
	{"kan",0,0,0},
	{"kang",0,0,0},
	{"kao",0,0,0},
	{"ke",0,0,0},
	{"kei",0,0,0},
	{"ken",0,0,0},
	{"keng",0,0,0},
	{"kong",0,0,0},
	{"kou",0,0,0},
	{"ku",0,0,0},
	{"kua",0,0,0},
	{"kuai",0,0,0},
	{"kuan",0,0,0},
	{"kuang",0,0,0},
	{"kui",0,0,0},
	{"kun",0,0,0},
	{"kuo",0,0,0},
	{"la",0,0,0},
	{"lai",0,0,0},
	{"lan",0,0,0},
	{"lang",0,0,0},
	{"lao",0,0,0},
	{"le",0,0,0},
	{"lei",0,0,0},
	{"leng",0,0,0},
	{"li",0,0,0},
	{"lia",0,0,0},
	{"lian",0,0,0},
	{"liang",0,0,0},
	{"liao",0,0,0},
	{"lie",0,0,0},
	{"lin",0,0,0},
	{"ling",0,0,0},
	{"liu",0,0,0},
	{"lo",0,0,0},
	{"long",0,0,0},
	{"lou",0,0,0},
	{"lu",0,0,0},
	{"luan",0,0,0},
	{"lun",0,0,0},
	{"luo",0,0,0},
	{"lv",0,0,0},
	{"lve",0,0,0},
	{"ma",0,0,0},
	{"mai",0,0,0},
	{"man",0,0,0},
	{"mang",0,0,0},
	{"mao",0,0,0},
	{"me",0,0,0},
	{"mei",0,0,0},
	{"men",0,0,0},
	{"meng",0,0,0},
	{"mi",0,0,0},
	{"mian",0,0,0},
	{"miao",0,0,0},
	{"mie",0,0,0},
	{"min",0,0,0},
	{"ming",0,0,0},
	{"miu",0,0,0},
	{"mo",0,0,0},
	{"mou",0,0,0},
	{"mu",0,0,0},
	{"na",0,0,0},
	{"nai",0,0,0},
	{"nan",0,0,0},
	{"nang",0,0,0},
	{"nao",0,0,0},
	{"ne",0,0,0},
	{"nei",0,0,0},
	{"nen",0,0,0},
	{"neng",0,0,0},
	{"ni",0,0,0},
	{"nian",0,0,0},
	{"niang",0,0,0},
	{"niao",0,0,0},
	{"nie",0,0,0},
	{"nin",0,0,0},
	{"ning",0,0,0},
	{"niu",0,0,0},
	{"nong",0,0,0},
	{"nou",0,0,0},
	{"nu",0,0,0},
	{"nuan",0,0,0},
	{"nuo",0,0,0},
	{"nv",0,0,0},
	{"nve",0,0,0},
	{"o",0,0,0},
	{"ou",0,0,0},
	{"pa",0,0,0},
	{"pai",0,0,0},
	{"pan",0,0,0},
	{"pang",0,0,0},
	{"pao",0,0,0},
	{"pei",0,0,0},
	{"pen",0,0,0},
	{"peng",0,0,0},
	{"pi",0,0,0},
	{"pian",0,0,0},
	{"piao",0,0,0},
	{"pie",0,0,0},
	{"pin",0,0,0},
	{"ping",0,0,0},
	{"po",0,0,0},
	{"pou",0,0,0},
	{"pu",0,0,0},
	{"qi",0,0,0},
	{"qia",0,0,0},
	{"qian",0,0,0},
	{"qiang",0,0,0},
	{"qiao",0,0,0},
	{"qie",0,0,0},
	{"qin",0,0,0},
	{"qing",0,0,0},
	{"qiong",0,0,0},
	{"qiu",0,0,0},
	{"qu",0,0,0},
	{"quan",0,0,0},
	{"que",0,0,0},
	{"qun",0,0,0},
	{"ran",0,0,0},
	{"rang",0,0,0},
	{"rao",0,0,0},
	{"re",0,0,0},
	{"ren",0,0,0},
	{"reng",0,0,0},
	{"ri",0,0,0},
	{"rong",0,0,0},
	{"rou",0,0,0},
	{"ru",0,0,0},
	{"ruan",0,0,0},
	{"rui",0,0,0},
	{"run",0,0,0},
	{"ruo",0,0,0},
	{"sa",0,0,0},
	{"sai",0,0,0},
	{"san",0,0,0},
	{"sang",0,0,0},
	{"sao",0,0,0},
	{"se",0,0,0},
	{"sen",0,0,0},
	{"seng",0,0,0},
	{"sha",0,0,0},
	{"shai",0,0,0},
	{"shan",0,0,0},
	{"shang",0,0,0},
	{"shao",0,0,0},
	{"she",0,0,0},
	{"shei",0,0,0},
	{"shen",0,0,0},
	{"sheng",0,0,0},
	{"shi",0,0,0},
	{"shou",0,0,0},
	{"shu",0,0,0},
	{"shua",0,0,0},
	{"shuai",0,0,0},
	{"shuan",0,0,0},
	{"shuang",0,0,0},
	{"shui",0,0,0},
	{"shun",0,0,0},
	{"shuo",0,0,0},
	{"si",0,0,0},
	{"song",0,0,0},
	{"sou",0,0,0},
	{"su",0,0,0},
	{"suan",0,0,0},
	{"sui",0,0,0},
	{"sun",0,0,0},
	{"suo",0,0,0},
	{"ta",0,0,0},
	{"tai",0,0,0},
	{"tan",0,0,0},
	{"tang",0,0,0},
	{"tao",0,0,0},
	{"te",0,0,0},
	{"teng",0,0,0},
	{"ti",0,0,0},
	{"tian",0,0,0},
	{"tiao",0,0,0},
	{"tie",0,0,0},
	{"ting",0,0,0},
	{"tong",0,0,0},
	{"tou",0,0,0},
	{"tu",0,0,0},
	{"tuan",0,0,0},
	{"tui",0,0,0},
	{"tun",0,0,0},
	{"tuo",0,0,0},
	{"wa",0,0,0},
	{"wai",0,0,0},
	{"wan",0,0,0},
	{"wang",0,0,0},
	{"wei",0,0,0},
	{"wen",0,0,0},
	{"weng",0,0,0},
	{"wo",0,0,0},
	{"wu",0,0,0},
	{"xi",0,0,0},
	{"xia",0,0,0},
	{"xian",0,0,0},
	{"xiang",0,0,0},
	{"xiao",0,0,0},
	{"xie",0,0,0},
	{"xin",0,0,0},
	{"xing",0,0,0},
	{"xiong",0,0,0},
	{"xiu",0,0,0},
	{"xu",0,0,0},
	{"xuan",0,0,0},
	{"xue",0,0,0},
	{"xun",0,0,0},
	{"ya",0,0,0},
	{"yan",0,0,0},
	{"yang",0,0,0},
	{"yao",0,0,0},
	{"ye",0,0,0},
	{"yi",0,0,0},
	{"yin",0,0,0},
	{"ying",0,0,0},
	{"yo",0,0,0},
	{"yong",0,0,0},
	{"you",0,0,0},
	{"yu",0,0,0},
	{"yuan",0,0,0},
	{"yue",0,0,0},
	{"yun",0,0,0},
	{"za",0,0,0},
	{"zai",0,0,0},
	{"zan",0,0,0},
	{"zang",0,0,0},
	{"zao",0,0,0},
	{"ze",0,0,0},
	{"zei",0,0,0},
	{"zen",0,0,0},
	{"zeng",0,0,0},
	{"zha",0,0,0},
	{"zhai",0,0,0},
	{"zhan",0,0,0},
	{"zhang",0,0,0},
	{"zhao",0,0,0},
	{"zhe",0,0,0},
	{"zhen",0,0,0},
	{"zheng",0,0,0},
	{"zhi",0,0,0},
	{"zhong",0,0,0},
	{"zhou",0,0,0},
	{"zhu",0,0,0},
	{"zhua",0,0,0},
	{"zhuai",0,0,0},
	{"zhuan",0,0,0},
	{"zhuang",0,0,0},
	{"zhui",0,0,0},
	{"zhun",0,0,0},
	{"zhuo",0,0,0},
	{"zi",0,0,0},
	{"zong",0,0,0},
	{"zou",0,0,0},
	{"zu",0,0,0},
	{"zuan",0,0,0},
	{"zui",0,0,0},
	{"zun",0,0,0},
	{"zuo",0,0,0},
	{"\0",0,0,0}
};

/*
* 根据配置文件
*
*/
int py_init(py_init_t * config)
{


}

/*
* sm_table一些东西需要动态初始化,在这里完成
* 主要通过py_table完成索引.
*/
int fill_sm_table(void)
{
	int i=0;
	short len=0;
	char c='\0';

	for( i=0; py_table[i].py_str[0] != '\0' ; i++)
	{
		if( py_table[i].py_str[0] != c)
		{
			c = py_table[i].py_str[0];
			sm_table[GET_SM_INDEX(c)].start = i;
		}

		len = strlen(py_table[i].py_str);
		if( len > sm_table[GET_SM_INDEX(c)].max_len )
		{
			sm_table[GET_SM_INDEX(c)].max_len = len;
		}

		sm_table[GET_SM_INDEX(c)].end=i;
	}

	return 0;
}

/*
* 拼音是否是合法的拼音
*/
int is_in_py_table(char * str,int left,int right)
{
	int middle;
	
	while(left <= right)
	{
		middle = (left+right)/ 2;
		if( (strcmp( py_table[middle].py_str,str) >0 ) )
		{
			right = middle-1;
		}
		else if( (strcmp(py_table[middle].py_str,str) <0 ) )
		{
			left = middle+1;
		}
		else
		{
			return middle;
		}
		count++;
	}

	return -1;
}

int char_type(char c)
{
	if( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'v')
	{
		return 0;
	}

	return 1;

}


//解析拼音串
int parse_py_string(char * str)
{
	int i=0;
	int index=0;
	int len;
	char val=0;
	char buf[16]={0};
	char tmp[128]={0};
		
	len=strlen(str);

	while( str[index] != '\0' )
	{
		memset(buf,0,sizeof(buf));
		if( is_sm( str[index] ) )
		{
			if( index >= len-1 )
			{
				tmp[strlen(tmp)]=str[index];
				break;
			}

			for( i = sm_table[GET_SM_INDEX(str[index])].max_len ; i > 0 ; i-- )
			{
				memset(buf,0,sizeof(buf));
				if( index + i > len)
				{
					continue;
				}
				//printf("%s %d %d %d\n",strncpy( buf,&str[index] ,i),i,sm_table[GET_SM_INDEX(str[index])].start,sm_table[GET_SM_INDEX(str[index])].end);
				if( is_in_py_table( strncpy( buf,&str[index] ,i),sm_table[GET_SM_INDEX(str[index])].start,sm_table[GET_SM_INDEX(str[index])].end ) >= 0)
				{
					strcat(tmp, buf);
					strcat(tmp,"'");
					index = index+i-1;
					break;
				}
			}
			if( strlen(buf) == 1)
			{
				tmp[strlen(tmp)]=str[index];
				strcat(tmp,"'");
				//printf("%c\n",str[index]);
			}

			if( index == len-1)
			{
				break;
			}
			else
			{
				index++;
			}
		}
		else
		{
			if( index >= len-1 )
			{
				tmp[strlen(tmp)]=str[index];
				break;
			}

			for( i = sm_table[GET_SM_INDEX(str[index])].max_len ; i > 0 ; i-- )
			{
				if( index + i > len)
				{
					continue;
				}

				if( is_in_py_table( strncpy( buf,&str[index] ,i),sm_table[GET_SM_INDEX(str[index])].start,sm_table[GET_SM_INDEX(str[index])].end ) >0)
				{
					printf(" %s ", buf);
					index =+i;
					break;
				}

				tmp[strlen(tmp)]=str[index];
				//当韵母分拆的时候遇到u v的时候,分拆停止.
				//return -1;
			}

			if( index == len-1)
			{
				break;
			}
			else
			{
				index++;
			}
		}
	}
	printf("%s \n",tmp);
	printf("%d \n",count);
	return 0;
}
